//
//  IMtextMessageTableViewCell.m
//  markmap
//
//  Created by 甘世清 on 2024/5/28.
//

#import "IMtextMessageTableViewCell.h"

@implementation IMtextMessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self.bgView = [[UIView alloc]init];
    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.masksToBounds = YES;
   
    [self.contentView addSubview:self.bgView];
    
    [self.contentView addSubview:self.messageTextView];
    [self.messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView).offset(6);
        make.left.equalTo(self.contentView).offset(10+12+48);
    }];
    WS(weakSelf);
    self.messageTextView.showTextMenu = ^(CGRect startRect, CGRect endRect, NSRange selectRange, BOOL isSelectAll) {
        [weakSelf showMenuWithStartRect:startRect endRect:endRect isAll:isSelectAll];
    };
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.messageTextView).insets(UIEdgeInsetsMake(-4, -6, -4, -6));
    }];
    [self.contentView addSubview:self.blackImageView];
    [self.blackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(24);
        make.right.equalTo(self.bgView.mas_left).offset(-4);
        make.bottom.equalTo(self.bgView.mas_bottom);
    }];
    
    
}

- (void)setModel:(IMMessageModel *)model{
    [super setModel:model];
  
    self.messageTextView.text = model.content;
    [self.messageTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (model.sendType) {
            make.left.equalTo(self.contentView).offset(12+48+10 + 6);
        } else {
            make.left.equalTo(self.contentView).offset(SCREEN_WIDETH-12-48-10-model.cellSize.width-6);
        }
        make.width.mas_equalTo(model.cellSize.width);
        make.height.mas_equalTo(model.cellSize.height);
    }];
    if (self.model.sendType) {
        self.bgView.backgroundColor = SQColor(@"#F0F8FF");
        self.messageTextView.textColor = UIColor.blackColor;
    } else {
        self.bgView.backgroundColor = SQColor666666;
        self.messageTextView.textColor = UIColor.whiteColor;
//        self.bgView.backgroundColor = SQColor(@"#00CD66");
//        self.messageTextView.textColor = UIColor.whiteColor;
    }
     [self.contentView layoutIfNeeded];
}

- (void)showMenuWithStartRect:(CGRect)startRect endRect:(CGRect)endRect isAll:(BOOL)isAll
{
    // 显示菜单栏
    SJPopMenuItem *copy = [SJPopMenuItem itemWithType:SJPopMenuItemCopy];
    SJPopMenuItem *selectAll = [SJPopMenuItem itemWithType:SJPopMenuItemSelectAll];
    NSMutableArray *items = [NSMutableArray array];
    [items addObject:copy];//复制
    if (!isAll) {
        [items addObject:selectAll]; //全选
    }
    SJPopMenu *menu = [SJPopMenu menu];
    if (isAll) {
        [menu showBy:self.messageTextView withItems:items];
    } else {
        [menu showBy:self.messageTextView startRect:startRect endRect:endRect withItems:items];
    }
    __weak typeof(self) weakSelf = self;
    menu.itemActions = ^(SJPopMenuItemType type, NSString *title) {
        switch (type) {
            case SJPopMenuItemCopy://复制
                [weakSelf coppy];
                break;
            case SJPopMenuItemWithdraw: //撤回
                
                break;
            case SJPopMenuItemDelete: //删除
                
                break;
            case SJPopMenuItemSelectAll: //全选
                [weakSelf selectAllText];
                break;
            default:
                break;
        }
    };
}

- (void)coppy
{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
      pab.string = [self.model.content substringWithRange:self.messageTextView.selectedRange];
      if (pab == nil) {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制失败")];
      } else {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制成功")];
      }
 
}

- (void)selectAllText
{
   NSLog(@"%s", __func__);
  self.messageTextView.selectedRange = NSMakeRange(0, self.messageTextView.text.length);
}


- (SJCustomSelectTextView *)messageTextView{
    if (!_messageTextView) {
        _messageTextView = [[SJCustomSelectTextView alloc]init];
        _messageTextView.backgroundColor = UIColor.clearColor;
//        _messageTextView.layer.cornerRadius = 20;
//        _messageTextView.layer.masksToBounds = YES;
        _messageTextView.font = FONT_MEDIUM(15);
        _messageTextView.textColor = UIColor.whiteColor;
        _messageTextView.editable = NO;
        _messageTextView.selectable = YES;
//        _messageTextView.userInteractionEnabled = NO;
    }
    return _messageTextView;
}
@end
